import { login, reg } from '@/api/login'
import { setTokenTime, setTokenToStorage, getTokenFormStorage } from '@/utils/storage'

const state = () => {
  return {
    token: getTokenFormStorage() || null,
    userProfile: ''
  }
}

const mutations = {
  changeToken (state, payload) {
    state.token = payload
  },
  changeProfile (state, payload) {
    state.userProfile = payload
  }
}

const actions = {
  async userLogin (ctx, payload) {
    // 登录
    await login(payload).then(res => {
      // 存储token
      ctx.commit('changeToken', res.token)
      setTokenToStorage(res.token)

      // 存储时间戳
      setTokenTime()
    })
  },
  async userReg (ctx, payload) {
    await reg(payload).then(res => {
      console.log(res)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

// 界面单击 ==> 调用actions方法 ==> 传递数据 ==> 根据数据的repassword判断请求哪个接口
// 登录 ==> 登录成功 ==> 存储token
// 注册 ==> 注册成功 ==> 提示 ==> 切换到登录页(修改showTitle的值)
